package top.haijunit.navi.auth.controller;

import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.haijunit.framework.core.domain.Result;
import top.haijunit.framework.validation.ValidChecks;
import top.haijunit.navi.auth.domain.PermissionGroupInfo;
import top.haijunit.navi.auth.domain.PermissionInfo;
import top.haijunit.navi.auth.domain.PermissionRelationInfo;
import top.haijunit.navi.auth.entity.PermissionRelationEntity;
import top.haijunit.navi.auth.service.PermissionGroupService;
import top.haijunit.navi.auth.service.PermissionService;

import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * 权限管理
 *
 * @author zhanghaijun
 * @since 2024-08-20
 */
@Slf4j
@RestController
@RequestMapping("/access/permission")
@Tag(name = "权限管理")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class PermissionController {

    private final PermissionService permissionService;
    private final PermissionGroupService groupService;

    @ApiOperationSupport(order = 5)
    @Operation(summary = "权限组列表")
    @GetMapping("/group/list")
    public List<PermissionGroupInfo> permissionGroupList() {
        return groupService.selectList();
    }

    @ApiOperationSupport(order = 8)
    @Operation(summary = "权限组新增")
    @PostMapping("/group/add")
    public Result<Long> permissionGroupAdd(@RequestBody @Validated(ValidChecks.GroupAdd.class) PermissionGroupInfo info) {
        return Result.success(groupService.insert(info));
    }

    @ApiOperationSupport(order = 9)
    @Operation(summary = "权限组删除")
    @PostMapping("/group/delete/{code}")
    public Result<Long> permissionGroupDelete(@PathVariable Long code) {
        return Result.success(groupService.delete(code));
    }

    @ApiOperationSupport(order = 10)
    @Operation(summary = "权限列表")
    @GetMapping("/list")
    public List<PermissionInfo> getAllPermission(PermissionInfo permission) {
        return permissionService.selectList(permission);
    }

    @ApiOperationSupport(order = 11)
    @Operation(summary = "权限关系列表")
    @GetMapping("/relation")
    public List<PermissionRelationEntity> getPermissionRelationList() {
        return permissionService.getPermissionRelationList();
    }

    @ApiOperationSupport(order = 12)
    @Operation(summary = "权限导出")
    @GetMapping("/export")
    public void permissionExport(PermissionInfo permission, HttpServletResponse response) throws IOException {
        // 设置响应头
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment;filename*=utf-8''" + URLEncoder.encode("权限列表", StandardCharsets.UTF_8) + ".xlsx");
        // 创建 Cell 居中样式
        WriteCellStyle writeCellStyle = new WriteCellStyle();
        writeCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        writeCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        // 数据写入
        EasyExcelFactory.write(response.getOutputStream(), PermissionInfo.class)
            .sheet("权限列表")
            .registerWriteHandler(new HorizontalCellStyleStrategy(null, writeCellStyle))
            .doWrite(permissionService.selectList(permission));
    }

    @ApiOperationSupport(order = 15)
    @Operation(summary = "权限新增")
    @PostMapping("/add")
    public Result<Long> add(@RequestBody @Validated(ValidChecks.GroupAdd.class) PermissionRelationInfo info) {
        Long code = permissionService.insert(info);
        return Result.success(code);
    }

    @ApiOperationSupport(order = 20)
    @Operation(summary = "权限删除")
    @DeleteMapping("/delete/{code}")
    public Result<Long> delete(@PathVariable Long code) {
        return Result.success(permissionService.delete(code));
    }
}

